<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/5
 * Time: 16:58
 */

namespace app\admin\controller;


use think\Db;

class Feedback extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $user_keyword = input('user_keyword');
            $fd_status = input('fd_status');
            $start_time = input('start_time');
            $end_time = input('end_time');
            
            $where = [];
            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_username|user_id|fd_phone'] = ['like', '%' . $user_keyword . '%'];
            }
            if(isset($fd_status) && $fd_status != ''){
                $where['fd_status'] = $fd_status;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['fd_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['fd_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['fd_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('feedback')
                ->join('up_u_user', 'fd_user_id = user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('feedback')
                ->join('up_u_user', 'fd_user_id = user_id', 'left')
                ->field('fd_id, user_id, user_username, fd_phone, fd_content, fd_time, fd_status')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('fd_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 设置已回复
     * @return \think\response\Json
     */
    public function feedbackReply(){
        $fd_id = input('id');
        $res = Db::table('up_feedback')
            ->where('fd_id', $fd_id)
            ->update([
                'fd_status' => 2,
                'fd_done_time' => date('Y-m-d H:i:s')
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '设置成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 删除反馈
     * @return \think\response\Json
     */
    public function feedbackDel(){
        $fd_id = input('id');
        $res = Db::table('up_feedback')->delete($fd_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}